package com.kinghunt.misp.service.impl.userx;

import com.kinghunt.misp.common.constant.OrganizationConstant;
import com.kinghunt.misp.common.constant.PermissionConstant;
import com.kinghunt.misp.dal.dao.userx.OrganizationDAO;
import com.kinghunt.misp.dal.dao.userx.PermissionDAO;
import com.kinghunt.misp.dal.dao.userx.SysInitInfoDAO;
import com.kinghunt.misp.dal.model.userx.OrganizationDO;
import com.kinghunt.misp.dal.model.userx.PermissionDO;
import com.kinghunt.misp.dal.model.userx.SysInitInfoDO;
import com.kinghunt.misp.service.api.userx.SystemToolService;
import com.bygoodtech.kings.common.dto.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description 系统
 * @Author jiangtao
 * @Date 2023/7/5 22:22
 */
@Service
public class SystemToolServiceImpl implements SystemToolService {
    @Autowired(required = false)
    SysInitInfoDAO sysInitInfoDAO;
    @Autowired(required = false)
    OrganizationDAO organizationDAO;
    @Autowired(required = false)
    PermissionDAO permissionDAO;
    @Override
    public ApiResult<Boolean> initSys(String domain) {
        String appCode = domain.toUpperCase();
        SysInitInfoDO sysInitInfo = new SysInitInfoDO();
        sysInitInfo.setDomain(domain);
        sysInitInfo.setAppCode(appCode);
        int ret = sysInitInfoDAO.insertSelective(sysInitInfo);
        if (ret == 1) {
            OrganizationDO organization = new OrganizationDO();
            organization.setAppCode(appCode);
            organization.setParentId(OrganizationConstant.ORGANIZATION_ANCESTOR_PARENT_ID);
            organization.setCode("init");
            organization.setName("init");
            organization.setDescription("init");
            organization.setLevel(0);
            organization.setStatus(0);
            organizationDAO.insertSelective(organization);

            PermissionDO permission = new PermissionDO();
            permission.setAppCode(appCode);
            permission.setParentId(PermissionConstant.PERMISSION_ANCESTOR_PARENT_ID);
            permission.setCode("init");
            permission.setName("init");
            permission.setDescription("init");
            permission.setType(0);
            permissionDAO.insertSelective(permission);
        }

        return ApiResult.ok(true);
    }
}
